/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.databox.custompersistence;

import java.sql.Timestamp;
import java.util.Date;

import twaver.ClientPropertyPersistentFilter;
import twaver.DataBoxOutputSetting;
import twaver.Element;
import twaver.FloatInsets;
import twaver.PersistenceManager;
import twaver.TDataBox;

public class CustomPersistenceDemo {

	public static void main(String[] args) throws Exception {
		String id = "PersistenceNodeID";
		PersistenceNode node = new PersistenceNode(id);
		
		// set javabean properties
		node.setName("ServaSoftware");
		node.setTag("2048950");
		Attribute attribute = new Attribute();
		attribute.setAttribute1("attribute1");
		attribute.setAttribute2(2);
		attribute.setAttribute3(new Date());
		node.setAttribute(attribute);
		
		// set client properties
		node.putClientProperty("T.key1", "T.value1");
		node.putClientProperty("T.key2", "T.value2");
		node.putClientProperty("T.key3", "T.value3");
		node.putClientProperty("P.key1", "P.value1");
		node.putClientProperty("P.key2", "P.value2");
		node.putClientProperty("P.key3", "P.value3");
		node.putClientProperty("Timestamp", new Timestamp(new Date().getTime()));

		// set user properties
		node.putUserProperty("U.key1", "U.value1");
		node.putUserProperty("U.key2", "U.value2");
		node.putUserProperty("U.key3", "U.value3");
		node.putUserProperty("U.key4", new FloatInsets(1.1f,2.2f,3.3f,4.4f));
		
		// set business data
		BusinessData data = new BusinessData();
		data.setEquipmentID(679);
		data.setEquipmentName("BSC");
		data.setManufacturerName("Ericsson");
		node.setBusinessObject(data);
		
		TDataBox box = new TDataBox();
		box.setID("ID-679679");
		box.setName("www.servasoftware.com");
		box.addElement(node);
		box.putClientProperty("save time", new Date().toString());
		box.putClientProperty("element count", new Integer(box.size()));
		box.putClientProperty("demo name", "Custom Persistence Demo");
		
		String fileName = "file:/D:/CustomPersistenceDemo.xml";
		
		// register Attribute class's delegate to prevent Attribute object is outputted to xml.
		PersistenceManager.registerClassDelegate(Attribute.class, PersistenceManager.TRANSIENT_DELEGATE);
		
		DataBoxOutputSetting setting = new DataBoxOutputSetting();
		
		setting.setWithElementId(true);
		setting.setWithBusinessObject(true);
		setting.setWithUserProperty(true);
		setting.setWithDataBoxClientProperty(true);
		
		// set the filter to filter the client properties with the key starts with "T."  
		setting.setClientPropertyFilter(new ClientPropertyPersistentFilter(){
			public boolean isTransient(Element element, Object clientPropertyKey) {
				if(clientPropertyKey.toString().startsWith("T.")){
					return true;
				}
				return false;
			}
		});
		
		box.output(fileName, setting);
		
		box = new TDataBox();
		box.parse(fileName);
		
		System.out.println(box.getID());
		System.out.println(box.getName());
		System.out.println(box.getClientProperty("save time"));
		System.out.println(box.getClientProperty("element count"));
		System.out.println(box.getClientProperty("demo name"));
		
		PersistenceNode element = (PersistenceNode)box.getElementByID(id);
		System.out.println("Attribute:" + element.getAttribute());
		System.out.println("BusinessData:" + element.getBusinessObject());
		System.out.println("ClientProperties:" + element.getClientProperties());
		System.out.println("UserProperties:" + element.getUserProperties());
		System.out.println("tag:" + element.getTag());
	}

}